package com.zh.kotlin_mvvm.ui

import android.annotation.SuppressLint
import android.content.res.Resources
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.NumberPicker
import androidx.annotation.RequiresApi
import com.blankj.utilcode.util.SizeUtils
import com.zh.common.base.BaseActivity
import com.zh.common.ktx.toast
import com.zh.kotlin_mvvm.R
import com.zh.kotlin_mvvm.databinding.ActivityNumberPickerBinding
import java.lang.IllegalArgumentException
import java.lang.reflect.Field

@RequiresApi(Build.VERSION_CODES.Q)
class NumberPickerActivity : BaseActivity<ActivityNumberPickerBinding>() {

    @SuppressLint("Range")
    override fun initView(savedInstanceState: Bundle?) {
        val list = arrayOf("a", "b", "c", "d", "e", "f", "g")
        viewBinding.hourpicker.minValue = 0
        viewBinding.hourpicker.maxValue = 6
        viewBinding.hourpicker.value = 5//当前需要选中那个位置
        viewBinding.hourpicker.displayedValues = list //设置需要显示的数组
        viewBinding.hourpicker.textColor = Color.BLUE//文字颜色
        viewBinding.hourpicker.textSize = SizeUtils.sp2px(18f).toFloat()//文字大小
        viewBinding.hourpicker.selectionDividerHeight = SizeUtils.dp2px(1f)//分割线高度
        viewBinding.hourpicker.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS//关闭编辑模式
        viewBinding.hourpicker.wrapSelectorWheel = true//是否循环
//        viewBinding.hourpicker.post {
//            setDividerColor(viewBinding.hourpicker)
//        }
        viewBinding.hourpicker.setFormatter {
            "${it}小时"
        }
        viewBinding.hourpicker.setOnValueChangedListener { picker, oldVal, newVal ->
            list[newVal].toast()
        }
        viewBinding.secodpicker.displayedValues = list //设置需要显示的数组
        viewBinding.secodpicker.minValue = 0
        viewBinding.secodpicker.maxValue = 6
//        setPickerDividerColor(viewBinding.secodpicker)
//        viewBinding.secodpicker.setNumberPickerDividerColor(viewBinding.secodpicker)
    }

    private fun setPickerDividerColor(picker: NumberPicker) {
        val pickerFields: Array<Field> = NumberPicker::class.java.declaredFields
        for (pf in pickerFields) {
            if (pf.name.equals("mSelectionDivider")) {
                pf.isAccessible = true
                try {
                    pf.set(picker, ColorDrawable(Color.BLUE))
                    picker.invalidate()
                } catch (e: IllegalAccessException) {
                    e.printStackTrace()
                } catch (e: Resources.NotFoundException) {
                    e.printStackTrace()
                } catch (e: IllegalArgumentException) {
                    e.printStackTrace()
                }
            }
        }
    }

    /**
     * 设置picker之间的间距
     */
    private fun setPickerMargin(
        picker: NumberPicker,
        left: Int = 0,
        top: Int = 0,
        right: Int = 0,
        bottom: Int = 0
    ) {
        val lp = picker.layoutParams as LinearLayout.LayoutParams
        lp.setMargins(left, top, right, bottom)
    }

    private fun setDividerColor(picker: NumberPicker, color: Int = Color.RED) {
        try {
            //获取私有方法
//            var field = NumberPicker::class.java.getDeclaredField("mSelectionDivider")
            val field = NumberPicker::class.java.declaredFields
            field.forEach {
                if (it.name == "mSelectionDivider") {
                    it.isAccessible = true
                    it.set(picker, ColorDrawable(resources.getColor(R.color.colorAccent)))
                }
            }
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        }
    }
}